package com.fzhucc.modules.lesson.rest;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.lesson.domain.LessonBanner;
import com.fzhucc.modules.lesson.domain.dto.LessonBannerQueryCriteria;
import com.fzhucc.modules.lesson.service.LessonBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author yyg
* @date 2022-12-07
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程:banner管理")
@RequestMapping("/api/lessonBanner")
public class LessonBannerController {

    private final LessonBannerService lessonBannerService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('lessonBanner:list')")
    public void exportLessonBanner(HttpServletResponse response, LessonBannerQueryCriteria criteria) throws IOException {
        lessonBannerService.download(lessonBannerService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询banner")
    @ApiOperation("查询banner")
    @AnonymousAccess
    public ResponseEntity<Object> queryLessonBanner(LessonBannerQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(lessonBannerService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增banner")
    @ApiOperation("新增banner")
    @PreAuthorize("@el.check('lessonBanner:add')")
    public ResponseEntity<Object> createLessonBanner(@Validated @RequestBody LessonBanner resources){
        return new ResponseEntity<>(lessonBannerService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改banner")
    @ApiOperation("修改banner")
    @PreAuthorize("@el.check('lessonBanner:edit')")
    public ResponseEntity<Object> updateLessonBanner(@Validated @RequestBody LessonBanner resources){
        lessonBannerService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除banner")
    @ApiOperation("删除banner")
    @PreAuthorize("@el.check('lessonBanner:del')")
    public ResponseEntity<Object> deleteLessonBanner(@RequestBody Integer[] ids) {
        lessonBannerService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}